function CoordinateConvert() 
{
	
}


CoordinateConvert.prototype.x_pi =  3.14159265358979324*3000.0/180.0;

//GCJ-02 to BD-09
CoordinateConvert.prototype.bd_encrypt =function( gg_lat,  gg_lon,  bd_lat,  bd_lon)
{
  var x = gg_lon, y = gg_lat;
  var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * this.x_pi);
  var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * this.x_pi);
  bd_lon = z * Math.cos(theta) + 0.0065;
  bd_lat = z * Math.sin(theta) + 0.006;
}
//BD-09 to GCJ-02
CoordinateConvert.prototype.bd_decrypt =function( bd, gg) // 参数为MapLatLon
{
	//bd_lat,  bd_lon,  gg_lat,  gg_lon
	
  var x = bd.lon - 0.0065, y = bd.lat - 0.006;
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * this.x_pi);
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * this.x_pi);
  gg.lon = z * Math.cos(theta);
  gg.lat = z * Math.sin(theta);
}




//世界坐标 转 火星坐标
CoordinateConvert.prototype. pi = 3.14159265358979324;
//
//Krasovsky 1940
//
//a = 6378245.0, 1/f = 298.3
//b = a * (1 - f)
//ee = (a^2 - b^2) / a^2;
CoordinateConvert.prototype.a = 6378245.0;
CoordinateConvert.prototype.ee = 0.00669342162296594323;

//
//World Geodetic System [WGS-84] ==> Mars Geodetic System [GCJ-02]
CoordinateConvert.prototype.transform = function( wgLat,  wgLon,   mgLat,   mgLon)
{
  if (outOfChina(wgLat, wgLon)==1)
  {
      mgLat = wgLat;
      mgLon = wgLon;
      return;
  }
   dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
   dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
   radLat = wgLat / 180.0 * pi;
   magic = Math.sin(radLat);
  magic = 1 - ee * magic * magic;
   sqrtMagic = Math.sqrt(magic);
  dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  mgLat = wgLat + dLat;
  mgLon = wgLon + dLon;
}

CoordinateConvert.prototype.outOfChina = function( lat,  lon)
{
  if (lon < 72.004 || lon > 137.8347)
      return 1;
  if (lat < 0.8293 || lat > 55.8271)
      return 1;
  return 0;
}


CoordinateConvert.prototype.transformLat = function( x,  y)
{
   ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(fabs(x));
  ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  return ret;
}

CoordinateConvert.prototype.transformLon = function( x,  y)
{
   ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(fabs(x));
  ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  return ret;
}

//测试版本， 地球坐标到百度坐标
CoordinateConvert.prototype.WGS84_to_BD09 = function( wgLat,  wgLon,   bdLat,   bdLon)
{
  // 地球坐标－》火星坐标
   mgLat, mgLon;
  transform(wgLat, wgLon,  mgLat,  mgLon);
  
  // 火星坐标－》百度坐标
  bd_encrypt(mgLat, mgLon, bdLat, bdLon);
  
}